[C#] How to create a constructor of a class that return a collection of instances of that class?

Posted by codemonkie on Stack Overflow See other posts from Stack Overflow or by codemonkie
Published on 2010-04-21T09:31:44Z Indexed on 2010/04/21 9:33 UTC
Read the original article Hit count: 223

My program has the following class definition:

public sealed class Subscriber
{
    private subscription;
    public Subscriber(int id)
    {
        using (DataContext dc = new DataContext())
        {
           this.subscription = dc._GetSubscription(id).SingleOrDefault();                
        }            
    }
}

,where

_GetSubscription() is a sproc which returns a value of type ISingleResult<_GetSubscriptionResult>

Say, I have a list of type List<int> full of 1000 ids and I want to create a collection of subscribers of type List<Subscriber>.

How can I do that without calling the constructor in a loop for 1000 times?

Since I am trying to avoid switching the DataContext on/off so frequently that may stress the database.

TIA.

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-sql